package com.xcity.game.shop;

import com.xcity.game.common.Currency;
import com.xcity.game.item.ItemTemplate;

import naga.x.App;
import naga.x.game.template.TemplateException;
import naga.x.game.template.TemplateService;
import naga.x.game.template.annotation.Excel;

/**
 * 商城物品表
 * @author yang.li
 *
 */
@Excel(value = "shop.xlsx", sheet = 0)
public class ShopItemTemplate extends AbstractShopTemplate {

	/** 所属商城(0:兑换商城; 1:道具商城; 2:宠物商城; 3:竞技场商城) */
	protected ShopType group;

	/** 货币类型(1:现金; 2:钻石) */
	protected Currency priceType;

	/** 促销数量 */
	protected int salesCount;
	
	/** 促销价格 */
	protected int salesPrice;

	/** 客户端商城列表是否可见 */
	protected boolean visible;
	
	/** 每日限购次数 */
	protected int numLimit;
	
	protected ItemTemplate item;
	private String uniqueId;
	
	public ShopType getGroup() {
		return group;
	}

	public void setGroup(ShopType group) {
		this.group = group;
	}

	public String getName() {
		return getItem().getName();
	}

	public String getDesc() {
		return getItem().getDesc();
	}

	public String getIcon() {
		return getItem().getIcon();
	}

	public Currency getPriceType() {
		return priceType;
	}

	public void setPriceType(Currency priceType) {
		this.priceType = priceType;
	}

	public int getSalesCount() {
		return salesCount;
	}

	public void setSalesCount(int salesCount) {
		this.salesCount = salesCount;
	}

	public int getSalesPrice() {
		return salesPrice;
	}

	public void setSalesPrice(int salesPrice) {
		this.salesPrice = salesPrice;
	}
	
	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}
	
	public int getNumLimit() {
		return numLimit;
	}

	public void setNumLimit(int numLimit) {
		this.numLimit = numLimit;
	}

	public ItemTemplate getItem() {
		return item != null ? item : (item = App.getApp().getServiceManager().get(TemplateService.class).get(ItemTemplate.class, id));
	}
	
	@Override
	public String getUniqueId() {
		return uniqueId != null ? uniqueId : (uniqueId = id + "_" + group.ordinal() + "_" + priceType.ordinal() + "_" + count);
	}

	@Override
	public void validate() throws TemplateException {
		if (priceType == null || priceType == Currency.UNKNOWN) {
			throw new TemplateException("shop.xlsx error: sheet=0, id=" + id);
		}
		if (salesCount <= 0) {
			salesCount = 1;
		}
		if (salesPrice <= 0) {
			salesPrice = price;
		}
		if (count <= 0) {
			count = salesCount;
		}
	}
}
